import { generateHighlightNode } from '@/utils/bizUtils';
export const modifySchemaInfoTableColumnsFn = (data) => {
  let { isView, getRenderByKeyword } = data
  let columns = [
    {
      title: 'SPU编码',
      key: 'spuNo',
      align: 'center',
    },
    {
      title: '商品名称',
      required: true,
      children: [
        {
          title: '原值',
          align: 'center',
          key: 'productNameFinal',
        },
        getPresentValueFn({
          isView,
          oldKey: 'productNameFinal',
          key: 'productNameFinalUpdate',
          attr: {
            type: 'inputChange'
          }
        })
      ],
    },
    {
      title: '关键词',
      required: true,
      children: [
        {
          title: '原值',
          key: 'keywordName',
        },
        {
          title: '现值',
          render: (h, { row }) => {
            return getRenderByKeyword(h, row);
          }
        }
      ],
    },
    {
      title: '助记码',
      children: [
        {
          title: '原值',
          align: 'center',
          key: 'helpCode',
        },
        getPresentValueFn({
          isView,
          oldKey: 'helpCode',
          key: 'helpCodeUpdate',
          attr: {
            type: 'inputChange',
            regular: /[\u4E00-\u9FA5]/g,
            width: 160
          }
        })
      ],
    },
    {
      title: '商品类型',
      required: true,
      children: [
        {
          title: '原值',
          align: 'center',
          key: 'productTypeStr',
        },
        getPresentValueFn({
          isView,
          optionsKey: 'productTypeOptions',
          oldKey: 'productType',
          key: 'productTypeUpdate',
          attr: {
            type: 'selectSpecial',
            options: 'productTypeOptions',
            width: 130
          }
        })
      ],
    },
    {
      title: '集散地',
      children: [
        {
          title: '原值',
          align: 'center',
          key: 'disCentre',
        },
        getPresentValueFn({
          isView,
          oldKey: 'disCentre',
          key: 'disCentreUpdate',
          attr: {
            type: 'input',
            width: 160
          }
        })
      ],
    },
    {
      title: '是否集采',
      required: true,
      children: [
        {
          title: '原值',
          align: 'center',
          key: 'centralBuyStateStr',
        },
        getPresentValueFn({
          isView,
          optionsKey: 'centralBuyStateOptions',
          oldKey: 'centralBuyState',
          key: 'centralBuyStateUpdate',
          attr: {
            type: 'selectSpecial',
            options: 'centralBuyStateOptions',
            width: 130
          }
        })
      ],
    },
    {
      title: '副标题',
      required: true,
      children: [
        {
          title: '原值',
          align: 'center',
          key: 'subtitle',
        },
        getPresentValueFn({
          isView,
          oldKey: 'subtitle',
          key: 'subtitleUpdate',
          attr: {
            type: 'input',
            width: 160
          }
        })
      ],
    },
    {
      title: '加价类型',
      required: true,
      children: [
        {
          title: '原值',
          align: 'center',
          key: 'addPriceTypeStr',
        },
        getPresentValueFn({
          // isView, // 产品让先彻底禁用此属性，以后再放开
          isView: true,
          optionsKey: 'addPriceTypeOptions',
          oldKey: 'addPriceType',
          key: 'addPriceTypeUpdate',
          attr: {
            type: 'selectSpecial',
            options: 'addPriceTypeOptions',
            width: 130
          }
        })
      ],
    },
    {
      title: '主计量单位',
      key: 'mainUnitStr',
      align: 'center',
    },
    {
      title: '品牌',
      key: 'brandName',
      align: 'center',
    },
    {
      title: '是否采购多计量',
      key: 'isPurchaseMultiUnitStr',
      align: 'center',
    },
    {
      title: '是否采销多计量',
      key: 'isSaleMultiUnitStr',
      align: 'center',
    }
  ]
  return columns
}
/* 获取现值 */
const getPresentValueFn = (data) => {
  let { isView, optionsKey, key, attr, oldKey } = data;
  /**
   * @viewCol 产看状态的现值
   * @editCol 可编辑状态的现值
  */
  let viewCol = {
    title: '现值',
    align: 'center',
    key,
    render: (h, { row }) => {
      return generateHighlightNode(h, row, oldKey)
    }
  }
  if (optionsKey) { // 如果是下拉框，需要进行回显操作特殊处理
    viewCol = {
      title: '现值',
      align: 'center',
      render: (h, { row }) => {
        return generateHighlightNode(h, row, oldKey, getSelectTextFn({ val: row[key], options: row[optionsKey] }))
      }
    }
  }
  let editCol = {
    title: '现值',
    align: 'center',
    key,
    ...attr
  }
  let result = isView ? viewCol : editCol
  return result;
}

/* select下拉框value对应label的回显方法 */
const getSelectTextFn = (data) => {
  let { val, options } = data;
  let result = val;
  let target = _.find(options, (item) => {
    return item.optionValue === val;
  });
  if (target) {
    result = target.optionHtml;
  }
  return result;
}
